package com.meida.boot.handler.app;


import com.meida.app.provider.handler.UserInfoHandler;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.RedisUtils;
import com.meida.module.pay.provider.service.PayBalanceService;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * APP用户操作相关扩展
 *
 * @author zyf
 */
@Component
@Log4j2
public class AppUserInfoHandler implements UserInfoHandler {

    @Resource
    private PayBalanceService payBalanceService;

    @Resource
    public RedisUtils redisUtils;

    @Resource
    private AppUserService appUserService;


    @Override
    public AppUser bindMobile(AppUser baseUser) {
        log.info("绑定手机号扩展,accountId:" + baseUser.getUserId());
        baseUser.setUserType("USER");
        return baseUser;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userReg(CriteriaSave cs) {
        Long userId = cs.getLong("userId");
        String shareCode = cs.getParams("inviter");
        if (FlymeUtils.isNotEmpty(shareCode)) {
            //查询推荐人
            AppUser appUser = appUserService.getUserByShareCode(shareCode);
            if (FlymeUtils.isNotEmpty(appUser)) {
                EntityMap map = redisUtils.getConfigMap("GLOBAL_CONFIG");
                //查询推荐金额
                BigDecimal recommendAmount = map.getBigDecimal("recommendAmount");
                if (FlymeUtils.gtzero(recommendAmount)) {
                    payBalanceService.addBindAmount(appUser.getUserId(), userId, recommendAmount);
                }
            }
        }
        payBalanceService.addUserBalance(userId);
        log.info("注册用户扩展");
    }


}
